<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{
            margin: 0;
        }
        nav{
            display: flex;
        }
        nav>div{
            flex: 1;
            text-align: center;
            height: 50px;
            line-height: 50px;
            position: relative;
        }
        .active::before{
            content: "";
            width: 30px;
            height: 3px;
            background-color: red;
            position: absolute;
            bottom: 2px;
            left: 50%;
            margin-left: -15px;
        }
    </style>
</head>
<body>
    <div id="root">

    </div>
</body>
<script src="./react.js"></script>
<script src="./react-dom.js"></script>
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
<script type="text/babel">
    
    class App extends React.Component{
            state = {
                navList:["首页","分类","购物车","我的"],
                // 当前选中的导航索引
                activeIndex:0
            }
            render(){
                return (
                    <div>
                     <nav>
                       {
                         this.state.navList.map((item,index)=>{
                            return (
                                <div key={index} className={this.state.activeIndex===index?'active':''} onClick={()=>{
                                    this.setState({
                                        activeIndex:index
                                    })
                                }}>{item}</div>
                            )
                         })
                       }
                     </nav> 
                    {
                        this.changeComponent()
                    }   
                    </div>
                )
            }
            changeComponent(){
                let {activeIndex} = this.state;
                if(activeIndex ===0){
                    return <Home/>
                }else if(activeIndex ===1){
                    return <Cate/>
                }else if(activeIndex ===2){
                    return <Cart/>
                }else{
                    return <Mine/>
                }
            }
    }
    class Home extends React.Component{
        render(){
            return (
                <h1>首页</h1>
            )
        }
    }
    class Cate extends React.Component{
        render(){
            return (
                <h1>分类</h1>
            )
        }
    }
    class Cart extends React.Component{
        render(){
            return (
                <h1>购物车</h1>
            )
        }
    }
    class Mine extends React.Component{
        render(){
            return (
                <h1>我的</h1>
            )
        }
    }
    ReactDOM.render(<App/>,document.getElementById("root"))
</script>
</html>